#include "EditeurEseo.h"

#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>

#include <QtDebug>

#include "Editor/EditWidget.h"
#include "Editor/MoveTool.h"
#include "Editor/CreateTool.h"
#include "Editor/CutTool.h"
#include "Editor/DoorTool.h"
#include "Editor/LightTool.h"

#include <QFileDialog>
#include <QDir>

#include "Model/Geometry.h"

#include <QToolBar>
#include "FileIO/FileIO.h"

EditeurEseo::EditeurEseo()
{
    widget = new Editor::EditWidget();
    setCentralWidget(widget);
    
    currentMap = new Model::BuildingMap;
    widget->setMesh(currentMap->walls());
    widget->setLights(currentMap->lights());
    
    createTool = new CreateTool(widget);
    moveTool = new MoveTool(widget);
    cutTool = new CutTool(widget);
    doorTool = new DoorTool(widget);
    lightTool = new LightTool(widget);
    
    
    connect(createTool, SIGNAL(sceneChanged()), widget, SLOT(updateScene()));
    connect(moveTool, SIGNAL(sceneChanged()), widget, SLOT(updateScene()));
    connect(cutTool, SIGNAL(sceneChanged()), widget, SLOT(updateScene()));
    connect(doorTool, SIGNAL(sceneChanged()), widget, SLOT(updateScene()));
    connect(lightTool, SIGNAL(sceneChanged()), widget, SLOT(updateScene()));
    
    widget->setTool(createTool);
    

    QAction* open = new QAction("Open...", this);
    QAction* saveAs = new QAction("Save As...", this);
    QAction* save = new QAction("Save", this);
    QAction* setBackgroundImage = new QAction("Set Background Image...", this);
    QAction* quit = new QAction("Quit", this);

    connect(open, SIGNAL(triggered()), SLOT(showOpenFileDialog()));
    connect(saveAs, SIGNAL(triggered()), SLOT(saveAs()));
    connect(save, SIGNAL(triggered()), SLOT(save()));
    connect(setBackgroundImage, SIGNAL(triggered()), SLOT(openBackgroundImageDialog()));
    connect(quit, SIGNAL(triggered()), SLOT(close()));

    QMenu* menu = menuBar()->addMenu("File");
    menu->addAction(open);
    menu->addAction(saveAs);
    menu->addAction(save);
    menu->addAction(setBackgroundImage);
    menu->addAction(quit);
    
    QToolBar *toolbar = new QToolBar(widget);
    toolbar->addAction("Move", this, SLOT(setMoveTool()));
    toolbar->addAction("Create", this, SLOT(setCreateTool()));
    toolbar->addAction("Cut", this, SLOT(setCutTool()));
    toolbar->addAction("Door", this, SLOT(setDoorTool()));
    toolbar->addAction("Light", this, SLOT(setLightTool()));

    addToolBar(toolbar);
}

EditeurEseo::~EditeurEseo()
{}

void EditeurEseo::openFile(const QString& filename)
{
    qDebug() << "Opening file" << filename;
    currentMap = new Model::BuildingMap;
    FileIO::loadBuildingMap((*currentMap), filename.toStdString());
    widget->setMesh(currentMap->walls());
    widget->setLights(currentMap->lights());
    currentFileName = filename;
    widget->setBackgroundImage(QString(currentMap->background().c_str()));
}

void EditeurEseo::showOpenFileDialog()
{
    QString fileName = QFileDialog::getOpenFileName(this,
     tr("Open Map File"), QDir::homePath(), tr("World Of ESEO Map Files (*.woe)"));
     openFile(fileName);
}

void EditeurEseo::save()
{
    FileIO::saveBuildingMap((*currentMap), currentFileName.toStdString());
}

void EditeurEseo::setCreateTool()
{
    widget->setTool(createTool);
}

void EditeurEseo::setMoveTool()
{
    widget->setTool(moveTool);
}

void EditeurEseo::setCutTool()
{
    widget->setTool(cutTool);
}

void EditeurEseo::setDoorTool()
{
    widget->setTool(doorTool);
}

void EditeurEseo::setLightTool()
{
    widget->setTool(lightTool);
}

void EditeurEseo::openBackgroundImageDialog()
{
    QString fileName = QFileDialog::getOpenFileName(this,
     tr("Open Background Image"), QDir::homePath(), tr("Image files"));
    widget->setBackgroundImage(fileName);
    currentMap->setBackground(fileName.toStdString());
}

void EditeurEseo::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this,
     tr("Open Background Image"), QDir::homePath(), tr("World Of ESEO Map Files (*.woe)"));
    currentFileName = fileName;
    save();
}
